package snail.controllers;

import java.awt.AlphaComposite;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.validation.BindException;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.multipart.support.ByteArrayMultipartFileEditor;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
import snail.misc.FileUploadBean;

/**
 *
 * @author Mickel
 */
public class PictureUploadController extends SimpleFormController {

    @Override
    protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) throws Exception {
        FileUploadBean bean = (FileUploadBean) command;
        byte[] file = bean.getFile();

        if (file == null) {
            // Gör inget
        } else {
            int id = Integer.parseInt(request.getParameter("id"));

            InputStream in = new ByteArrayInputStream(file);
            BufferedImage uploadedImage = ImageIO.read(in);

            writeImage(uploadedImage, true, id);
            writeImage(uploadedImage, false, id);

            response.sendRedirect("/Snail_Titanium/articles/details/" + id);
        }

        return super.onSubmit(request, response, command, errors);
    }

    public void writeImage(BufferedImage uploadedImage, boolean isThumbnail, int auctionId) throws FileNotFoundException, IOException {
        int width = 140, height = 90;
        String name = String.format("%s/%d", isThumbnail ? "Thumb" : "Big", auctionId);

        if (!isThumbnail) {
            width = 515;
            height = 900;
        }

        BufferedImage resizedImage = resizeImage(uploadedImage, width, height);
        File localFile = new File(getServletContext().getRealPath("/Auctions/Images/" + name + ".png"));

        FileOutputStream fo = new FileOutputStream(localFile);
        ImageIO.write(resizedImage, "PNG", fo);
        fo.close();
    }

    BufferedImage resizeImage(Image originalImage, int scaledWidth, int scaledHeight) {
        // Beräkna bredd och höjd
        double imageRatio = (double) originalImage.getWidth(null) / (double) originalImage.getHeight(null);
        int newHeight = (int) (scaledWidth / imageRatio);
        if (newHeight > scaledHeight) {
            newHeight = scaledHeight;
        }

        BufferedImage resizedImage = new BufferedImage(scaledWidth, newHeight, BufferedImage.TYPE_INT_RGB);
        Graphics2D g = resizedImage.createGraphics();
        g.setComposite(AlphaComposite.Src);
        g.drawImage(originalImage, 0, 0, scaledWidth, newHeight, null);
        g.dispose();
        return resizedImage;
    }

    @Override
    protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder)
            throws ServletException {
        binder.registerCustomEditor(byte[].class, new ByteArrayMultipartFileEditor());
    }
}

